<template>
  <div>
    <div>
      <FilterTable
        ref="table"
        height="210"
        columnsRef="partAccessDiffTabsBrandModal"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'rows'"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="saveData"
      >
      </FilterTable>
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInventory } from '@/api/parts/access/diff';
export default {
  name: 'BrandModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      fetchApi: queryPartInventory,
      fetchParams: {},
      columns: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    saveData(row) {
      if (row.length > 0) {
        for (let i = 0; i < row.length; i++) {
          this.arr.push(row[i].stockTakingNo);
        }
      }
      console.log(this.arr);
    },
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.brandId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '盘点单号',
          dataIndex: 'inerntoryNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '盘点日期',
          dataIndex: 'inerntoryName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经手人',
          dataIndex: 'employeeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作员',
          dataIndex: 'employeeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '锁定人',
          dataIndex: 'lockName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addBrand(this.saveData);
        }
        if (this.type === 'edit') {
          res = await updateBrand(this.saveData, this.recordId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped></style>
